package com.sinaapp.kltq.dinosaurweather;

import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.view.View;


/**
 * Created by dell on 2015/7/27.
 */
public class temperatureChart extends View {
    private Paint mPaint;
    private Paint mPointPaint;
    private Paint mTextPaint;
    private Paint mLinePaint;
    private Paint mbackLinePaint;

    private Context mContext;
    static float[] highTemper ;//高温
    static float[] lowTemper;//低温
    public float XLength;//X轴的长度
    public float XScale;//X刻度
    public float YLength;//Y轴的长度
    public float YScale;//Y刻度


    public temperatureChart(Context context){
        super(context);
    }
    public temperatureChart(Context context,AttributeSet attr){
        super(context,attr);
    }


    public static int sp2px(Context context, float spValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        XLength = getWidth();
        XScale = XLength/6;
        float space= XScale/2;
        ForecastFragment data = new ForecastFragment();

        mPaint = new Paint();
        mbackLinePaint = new Paint();
        mbackLinePaint.setColor(Color.WHITE);
        //point set
        mPointPaint = new Paint();
        mPointPaint.setAntiAlias(true);
        mPointPaint.setColor(Color.WHITE);

        //line set
        mLinePaint = new Paint();
        mLinePaint.setColor(Color.WHITE);
        mLinePaint.setAntiAlias(true);
        mLinePaint.setStrokeWidth(1);
        mLinePaint.setStyle(Style.FILL);

        //line
        mTextPaint = new Paint();
        mTextPaint.setAntiAlias(true);
        mTextPaint.setColor(Color.WHITE);
       /*mLinePaint.setTextSize((int)(18*(float)XLength/320));*/
        mLinePaint.setTextSize(sp2px(this.getContext(),18));

        /*data.getHigh();*/
        highTemper = new float[6];
        lowTemper = new float[6];
        highTemper = data.getHigh();
        lowTemper = data.getLow();

        //
        for (int i = 0; i < highTemper.length; i++) {

            if (i != highTemper.length-1 ) {

                canvas.drawText(highTemper[i]+"°C",i*XScale+space/2 , YCoord(highTemper[i]+1),
                        mTextPaint);
                canvas.drawLine(XScale * i+space, YCoord(highTemper[i]), XScale * (i+1)+space,
                        YCoord(highTemper[i+1]), mLinePaint);
                canvas.drawCircle(XScale * i+space, YCoord(highTemper[i]), 2, mPointPaint);
                canvas.drawCircle(XScale * (i + 1)+space, YCoord(highTemper[i+1]),2 ,
                        mPointPaint);
            } else {

                canvas.drawText(highTemper[i]+"°C",i*XScale+space/2 ,YCoord(highTemper[i]+1),
                        mTextPaint);
            }
        }

            for (int i = 0; i< lowTemper.length; i++) {

                if (i != lowTemper.length - 1) {

                    canvas.drawText(lowTemper[i]+"°C",i*XScale+space/2 , 20 + YCoord(lowTemper[i]),
                            mTextPaint);
                    canvas.drawLine(XScale * i+space, YCoord(lowTemper[i]), XScale * (i+1)+space,
                            YCoord(lowTemper[i+1]), mLinePaint);
                    canvas.drawCircle(XScale * i+space, YCoord(lowTemper[i]), 2, mPointPaint);
                    canvas.drawCircle(XScale * (i + 1)+space, YCoord(lowTemper[i+1]), 2,
                            mPointPaint);
                } else {

                    canvas.drawText(lowTemper[i]+"°C",i*XScale+space/2 , 20 + YCoord(lowTemper[i]),
                            mTextPaint);
                }

            }

    }
    private float YCoord(float y0)  //计算Y轴的高度
    {
        YLength = getHeight();
        YScale = YLength/40;
        float y;
        try
        {
            y=(y0);
        }
        catch(Exception e)
        {
            return -999;    //出现错误返回-999
        }
        try
        {
            return (40-y)*YScale;
        }
        catch(Exception e)
        {
        }
        return y;
    }

}
